const comp = require('miniprogram-computed').behavior
Component({
  behaviors:[comp],
  data: {
    tt:false,
    tab:[],
    jinyong:false
  },
  computed:{
    zong(data){
      let result = data.tab.reduce((total,item)=>{
        if(item.checked){
          return total+= item.price*item.count
        }
        return total;
    },0)
    return result;
    },
    tt(data){
     return data.tab.every(item=>{
        return item.checked==true
      })
    }
  },
  methods: {
    qx(){ 
     this.data.tab.forEach(item=>{
       return item.checked=!item.checked
     })
     this.setData({
       tab:this.data.tab
     })
    },
    dan(e){
        this.data.tab[e.currentTarget.dataset.id].checked=!this.data.tab[e.currentTarget.dataset.id].checked
        this.setData({
          tab:this.data.tab
        })
    },
    del(e){
      let id = e.currentTarget.dataset.id
        this.data.tab.splice(id,1)
       this.setData({
         tab:this.data.tab
        })
        wx.setStorageSync('cart', this.data.tab)
    },
    jia(e){
      let id = e.currentTarget.dataset.id
      this.data.tab[id].count+=1
      if (this.data.tab[id].count>=1) {
        this.setData({
          jinyong:false
        })
      }
      this.setData({
        tab:this.data.tab
      })
    },
    jian(e){ 
      let id = e.currentTarget.dataset.id
      if (this.data.tab[id].count<=1) {
        this.setData({
          jinyong:true
        })
      }
      this.data.tab[id].count-=1
      this.setData({
        tab:this.data.tab
      })
    }
  },
  pageLifetimes:{
    show(){
      this.setData({
        tab:wx.getStorageSync('cart')
      })
    }
  }
})